# -*- coding: utf-8 -*-
"""Sample controller module"""

# turbogears imports
from tg import expose
from tg import redirect, validate, flash

# third party imports
#from pylons.i18n import ugettext as _
#from repoze.what import predicates

# project specific imports
from crv.lib.base import BaseController
from tg.controllers import RestController
from crv.model import DBSession, metadata
from crv.model.crvmodel import *
from tgext.crud import CrudRestController

from sprox.tablebase import TableBase
from sprox.fillerbase import TableFiller, EditFormFiller
from sprox.formbase import EditableForm, AddRecordForm

#from sprox.dojo.tablebase import DojoTableBase 
#from sprox.dojo.fillerbase import DojoTableFiller
#from sprox.dojo.formbase import AddRecordForm

import logging
log = logging.getLogger(__name__)

            
class VMController(CrudRestController):
    model = VM

    class new_form_type(AddRecordForm):
        __model__ = VM
        __omit_fields__ = ['VM_ID']

    class edit_form_type(EditableForm):
        __model__ = VM
        __omit_fields__ = ['VM_ID']
        
    class edit_filler_type(EditFormFiller):
        __model__ = VM
        
    class table_type(TableBase):
        __model__ = VM
        __xml_fields__ = ['role']
        
    class table_filler_type(TableFiller):
        __model__ = VM
        def role(self, obj):
            roles = ', '.join(['<a href="/roles/'+str(d.Role_ID)+'/edit">'+d.name+'</a>'
                               for d in obj.role])
            return roles.join(('<div>', '</div>'))

        def attr(self,obj):
            attrs= ", ".join([iattr.name+'='+iattr.value for iattr in obj.attr])
            return attrs
        
        def currentSC(self,obj):
            return obj.SCname[obj.currentSC]

        def targetSC(self,obj):
            return obj.SCname[obj.targetSC]
        
class ClusterController(CrudRestController):
    model = Cluster

    class new_form_type(AddRecordForm):
        __model__ = Cluster
        __omit_fields__ = ['Cluster_ID']

    class edit_form_type(EditableForm):
        __model__ = Cluster
        __omit_fields__ = ['Cluster_ID']
        
    class edit_filler_type(EditFormFiller):
        __model__ = Cluster
        
    class table_type(TableBase):
        __model__ = Cluster
        __xml_fields__ = ['role']
        
    class table_filler_type(TableFiller):
        __model__ = Cluster
        def role(self, obj):
            roles = ', '.join(['<a href="/roles/'+str(d.Role_ID)+'/edit">'+d.name+'</a>'
                               for d in obj.role])
            return roles.join(('<div>', '</div>'))
        
        def attr(self,obj):
            attrs= ", ".join([iattr.name+'='+iattr.value for iattr in obj.attr])
            return attrs

        def currentSC(self,obj):
            return obj.SCname[obj.currentSC]

        def targetSC(self,obj):
            return obj.SCname[obj.targetSC]
        
class RoleController(CrudRestController):
    model = Role

    class new_form_type(AddRecordForm):
        __model__ = Role
        __omit_fields__ = ['Role_ID']

    class edit_form_type(EditableForm):
        __model__ = Role
        __omit_fields__ = ['Role_ID']
        
    class edit_filler_type(EditFormFiller):
        __model__ = Role
        
    class table_type(TableBase):
        __model__ = Role
        __xml_fields__ = ['vm','cluster','roleDef','dependOn']
        
    class table_filler_type(TableFiller):
        __model__ = Role
        def vm(self, obj):
            vms = '<a href="/vms/'+str(obj.vm.VM_ID)+'/edit">'+obj.vm.name+'</a>'
            return vms.join(('<div>', '</div>'))
        
        def roleDef(self, obj):
            roledefs = '<a href="/roledefs/'+str(obj.roleDef.RoleDef_ID)+'/edit">'+obj.roleDef.name+'</a>'
            return roledefs.join(('<div>', '</div>'))
        
        def cluster(self, obj):
            clusters = ', '.join(['<a href="/clusters/'+str(d.Cluster_ID)+'/edit">'+d.name+'</a>'
                               for d in obj.cluster])
            return clusters.join(('<div>', '</div>'))
        
        def dependOn(self, obj):
            dependons = ', '.join(['<a href="/roles/'+str(d.Role_ID)+'/edit">'+d.name+'</a>'
                               for d in obj.dependOn])
            return dependons.join(('<div>', '</div>'))
        
        def currentSC(self,obj):
            return obj.SCname[obj.currentSC]

        def targetSC(self,obj):
            return obj.SCname[obj.targetSC]

        def attr(self,obj):
            attrs= ", ".join([iattr.name+'='+iattr.value for iattr in obj.attr])
            return attrs

        
class RoleDefController(CrudRestController):
    model = RoleDef

    class new_form_type(AddRecordForm):
        __model__ = RoleDef
        __omit_fields__ = ['RoleDef_ID']

    class edit_form_type(EditableForm):
        __model__ = RoleDef
        __omit_fields__ = ['RoleDef_ID']
        
    class edit_filler_type(EditFormFiller):
        __model__ = RoleDef
        
    class table_type(TableBase):
        __model__ = RoleDef
        __xml_fields__=['dependon']
            
    class table_filler_type(TableFiller):
        __model__ = RoleDef
        

class ClusterAttrController(CrudRestController):
    model = ClusterAttr

    class new_form_type(AddRecordForm):
        __model__ = ClusterAttr
        __omit_fields__ = ['ClusterAttr_ID']

    class edit_form_type(EditableForm):
        __model__ = ClusterAttr
        __omit_fields__ = ['ClusterAttr_ID']
        
    class edit_filler_type(EditFormFiller):
        __model__ = ClusterAttr
        
    class table_type(TableBase):
        __model__ = ClusterAttr
        
    class table_filler_type(TableFiller):
        __model__ = ClusterAttr
        

class RoleAttrController(CrudRestController):
    model = RoleAttr

    class new_form_type(AddRecordForm):
        __model__ = RoleAttr
        __omit_fields__ = ['RoleAttr_ID']

    class edit_form_type(EditableForm):
        __model__ = RoleAttr
        __omit_fields__ = ['RoleAttr_ID']
        
    class edit_filler_type(EditFormFiller):
        __model__ = RoleAttr
        
    class table_type(TableBase):
        __model__ = RoleAttr
        
    class table_filler_type(TableFiller):
        __model__ = RoleAttr
        

class VMAttrController(CrudRestController):
    model = VMAttr

    class new_form_type(AddRecordForm):
        __model__ = VMAttr
        __omit_fields__ = ['VMAttr_ID']

    class edit_form_type(EditableForm):
        __model__ = VMAttr
        __omit_fields__ = ['VMAttr_ID']
        
    class edit_filler_type(EditFormFiller):
        __model__ = VMAttr
        
    class table_type(TableBase):
        __model__ = VMAttr
        
    class table_filler_type(TableFiller):
        __model__ = VMAttr
        
class StatusController(BaseController):

    @expose()
    def setVMAttr(self,vmid,name,value):
        vms = DBSession.query(VM).filter(VM.VM_ID==vmid).all()
        log.debug("StatusController::setVMAttr::vmid=%s name=%s, attr=%s"%(vmid,name,value))
        if len(vms)<1:
            return "False"
        vms[0].setAttr(name,value)
        return "True"
        
    @expose()
    def getVMAttr(self,vmid,name):
        vms = DBSession.query(VM).filter(VM.VM_ID==vmid).all()
        log.debug("StatusController::getVMAttr::vmid=%s name=%s"%(vmid,name))
        if len(vms)<1:
            return "None"
        return vms[0].getAttr(name)
        
    @expose()
    def setRoleAttr(self,roleid,name,value):
        roles = DBSession.query(Role).filter(Role.Role_ID==roleid).all()
        log.debug("StatusController::setRoleAttr::roleid=%s name=%s, attr=%s"%(roleid,name,value))
        if len(roles)<1:
            return "False"
        roles[0].setAttr(name,value)
        return "True"
        
    @expose()
    def getRoleAttr(self,roleid,name):
        roles = DBSession.query(Role).filter(Role.Role_ID==roleid).all()
        log.debug("StatusController::getRoleAttr::roleid=%s name=%s"%(roleid,name))
        if len(roles)<1:
            return "None"
        return roles[0].getAttr(name)
        
    @expose()
    def getDependentAttr(self,roleid,name):
        roles = DBSession.query(Role).filter(Role.Role_ID==roleid).all()
        log.debug("StatusController::getRoleAttr::roleid=%s name=%s"%(roleid,name))
        if len(roles)<1:
            return "None"
        result=[]
        for irole in roles[0].dependBy:
            attr=irole.getAttr(name)
            if attr!=None:
                result.append(result)
        if len(result)==0:
            return "None"
        return " ".join(a for a in result)
        
    @expose()
    def getProviderAttr(self,roleid,name):
        roles = DBSession.query(Role).filter(Role.Role_ID==roleid).all()
        log.debug("StatusController::getRoleAttr::roleid=%s name=%s"%(roleid,name))
        if len(roles)<1:
            return "None"
        
        result=[]
        for irole in roles[0].dependOn:
            attr=irole.getAttr(name)
            if attr!=None:
                result.append(attr)
        if len(result)==0:
            return "None"
        return " ".join(str(a) for a in result)
    
    @expose()
    def setClusterAttr(self,clusterid,name,value):
        clusters = DBSession.query(Cluster).filter(Cluster.Cluster_ID==clusterid).all()
        log.debug("StatusController::setClusterAttr::clusterid=%s name=%s, attr=%s"%(clusterid,name,value))
        if len(clusters)<1:
            return "False"
        clusters[0].setAttr(name,value)
        return "True"
        
    @expose()
    def getClusterAttr(self,clusterid,name):
        clusters = DBSession.query(Cluster).filter(Cluster.Cluster_ID==clusterid).all()
        log.debug("StatusController::getClusterAttr::clusterid=%s name=%s"%(clusterid,name))
        if len(clusters)<1:
            return "None"
        return clusters[0].getAttr(name)
        
